package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.domain.ZzExerciseBook;
import com.zzedu.domain.ZzPictureNode;
import com.zzedu.domain.ZzStudentLearnReport;
import com.zzedu.domain.ZzSubjectContent;
import com.zzedu.dto.ZzStudentLearnReportDTO;
import com.zzedu.mapper.ZzStudentLearnReportMapper;
import com.zzedu.service.IZzExerciseBookService;
import com.zzedu.service.IZzPictureNodeService;
import com.zzedu.service.IZzStudentLearnReportService;
import com.zzedu.service.IZzSubjectContentService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生学习报告Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-07-20
 */
@Service
public class ZzStudentLearnReportServiceImpl extends ServiceImpl<ZzStudentLearnReportMapper, ZzStudentLearnReport> implements IZzStudentLearnReportService {
    @Autowired
    IZzSubjectContentService zzSubjectContentService;

    @Autowired
    IZzExerciseBookService zzExerciseBookService;

    @Autowired
    IZzPictureNodeService zzPictureNodeService;

    @Override
    public LambdaQueryWrapper<ZzStudentLearnReport> buildQuery(ZzStudentLearnReportDTO zzStudentLearnReport) {
        LambdaQueryWrapper<ZzStudentLearnReport> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null != zzStudentLearnReport.getStuId(), ZzStudentLearnReport::getStuId, zzStudentLearnReport.getStuId());
        lambdaQueryWrapper.eq(null != zzStudentLearnReport.getReadFlag(), ZzStudentLearnReport::getReadFlag, zzStudentLearnReport.getReadFlag());
        lambdaQueryWrapper.eq(null != zzStudentLearnReport.getUnitId(), ZzStudentLearnReport::getUnitId, zzStudentLearnReport.getUnitId());
        lambdaQueryWrapper.eq(null != zzStudentLearnReport.getIsFinish(), ZzStudentLearnReport::getIsFinish, zzStudentLearnReport.getIsFinish());
        lambdaQueryWrapper.orderByDesc(ZzStudentLearnReport::getId);
        return lambdaQueryWrapper;
    }

    /**
     * 有没有全部做完该绘本作业
     *
     * @param bookId
     * @param stuId
     * @return
     */
    public boolean isFinish(Long bookId, Long stuId) {
        // 最后1个节点
        List<ZzPictureNode> zzPictureNodeDaysList = this.zzPictureNodeService.list(Wrappers.lambdaQuery(ZzPictureNode.class)
                .eq(ZzPictureNode::getPictureBookId, bookId)
                .eq(ZzPictureNode::getNodeType, 3)
                .orderByDesc(ZzPictureNode::getSortNo)
                .last(" limit 1 ")
        );

        if (CollectionUtils.isNotEmpty(zzPictureNodeDaysList)) {
            //倒叙处理节点
            List<ZzPictureNode> zzPictureNodeNodsList = this.zzPictureNodeService.list(Wrappers.lambdaQuery(ZzPictureNode.class)
                    .eq(ZzPictureNode::getNodeType, 2)
                    .in(ZzPictureNode::getpNodeId, zzPictureNodeDaysList.stream().map(ZzPictureNode::getId).collect(Collectors.toList()))
                    .orderByDesc(ZzPictureNode::getSortNo)
            );
            if (CollectionUtils.isNotEmpty(zzPictureNodeNodsList)) {
                //倒序节点中 第一道题做了 就算完成
                for (ZzPictureNode node : zzPictureNodeNodsList) {
                    ZzSubjectContent zzSubjectContent = zzSubjectContentService.getOne(Wrappers.lambdaQuery(ZzSubjectContent.class)
                            .eq(ZzSubjectContent::getNodeId, node.getId())
                            .last(" limit 1 ")
                            .orderByDesc(ZzSubjectContent::getSortNo)
                    );
                    if (zzSubjectContent == null) {
                        //没有题目 看后面节点
                        continue;
                    }
                    //学生完成数量
                    long answerCount = zzExerciseBookService.count(Wrappers.lambdaQuery(ZzExerciseBook.class)
                            .eq(ZzExerciseBook::getStudentId, stuId)
                            .eq(ZzExerciseBook::getContentId, zzSubjectContent.getId())
                            .eq(ZzExerciseBook::getPictureBookId, bookId)
                    );
                    return answerCount >= 1;
                }
            }
        }

/*        // 绘本下所有作业节点
        List<ZzPictureNode> zzPictureNodeDaysList = this.zzPictureNodeService.list(Wrappers.lambdaQuery(ZzPictureNode.class)
                .eq(ZzPictureNode::getPictureBookId, bookId)
                .eq(ZzPictureNode::getNodeType, 3)
        );
        if (CollectionUtils.isNotEmpty(zzPictureNodeDaysList)) {
            //获取天数下所有节点
            List<ZzPictureNode> zzPictureNodeNodsList = this.zzPictureNodeService.list(Wrappers.lambdaQuery(ZzPictureNode.class)
                    .eq(ZzPictureNode::getNodeType, 2)
                    .in(ZzPictureNode::getpNodeId, zzPictureNodeDaysList.stream().map(ZzPictureNode::getId).collect(Collectors.toList()))
            );
            if (CollectionUtils.isNotEmpty(zzPictureNodeNodsList)) {
                //题目数量
                long subjectCount = zzSubjectContentService.count(Wrappers.lambdaQuery(ZzSubjectContent.class)
                        .in(ZzSubjectContent::getNodeId, zzPictureNodeNodsList.stream().map(ZzPictureNode::getId).collect(Collectors.toList()))

                );
                if (subjectCount < 1) {
                    //没有题目 不生成
                    return false;
                }
                //学生完成数量
                long answerCount = zzExerciseBookService.count(Wrappers.lambdaQuery(ZzExerciseBook.class)
                        .eq(ZzExerciseBook::getStudentId, stuId)
                        .eq(ZzExerciseBook::getPictureBookId, bookId)
                );
                return answerCount >= subjectCount;
            }
        }*/
        return false;
    }
}